package com.ruoyi.project.remake.service;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.remake.domain.TRemake;
import com.ruoyi.project.remake.domain.constant.InitConstant;
import com.ruoyi.project.remake.domain.dto.LoginDoctorUser;
import com.ruoyi.project.remake.domain.dto.LoginRemake;
import com.ruoyi.project.remake.domain.dto.LoginSysUser;
import com.ruoyi.project.remake.domain.enmu.RemakeState;
import com.ruoyi.project.system.domain.SysRole;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysRoleMapper;
import com.ruoyi.project.system.service.ISysConfigService;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 登录校验方法
 *
 * @author ruoyi
 */
@Component
public class RemakeLoginService {
    @Autowired
    private RemakeTokenService remakeTokenService;

    @Autowired
    private ITRemakeService itRemakeService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private  SysUserTokenService sysUserTokenService;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private ISysConfigService configService;

    /**
     * 登录验证
     *
     * @param username 登录名
     *  @param password 密码
     *
     * @return 结果
                */
        public AjaxResult login(String username, String password) {
            AjaxResult ajax = AjaxResult.success();
            TRemake tRemake = itRemakeService.selectByUsername(username);
            if (!tRemake.getIsEnable().equals(RemakeState.ENABLE.getValue())) {
                return AjaxResult.error("该账户已禁用或者删除");
            }
        if (!passwordEncoder.matches(password, tRemake.getPassword())) {
            return AjaxResult.error("用户名或密码错误");
        }

        if (null != tRemake) {

            LoginRemake loginRemake = new LoginRemake();
            BeanUtils.copyProperties(tRemake, loginRemake);
            loginRemake.setId(tRemake.getId());
            loginRemake.setPassword(tRemake.getPassword());
            ajax.put(Constants.TOKEN, remakeTokenService.createToken(loginRemake));
            ajax.put(Constants.UUID, tRemake.getId());
            ajax.put(Constants.LOGIN_NAME,tRemake.getRemakeName());
            ajax.put(Constants.URL_SERVER, configService.selectConfigByKey(InitConstant.PRO_IMG_KEY));
            // 生成token
            return ajax;
        }
        return null;
    }

    /**
     *  C端用户登录操作   根据roleList的返回结果动态的加载功能按钮
     * @param userName
     * @param password
     * @return
     */
    public AjaxResult loginPrint(String userName, String password) {
        AjaxResult ajax = AjaxResult.success();
        SysUser sysUser=sysUserService.selectUserByUserName(userName);

        List<SysRole> sysRoleList=sysRoleMapper.selectRolePermissionByUserId(sysUser.getUserId());

        if(null==sysUser){
            return  AjaxResult.error("不存在该用户");
        }
        if (!passwordEncoder.matches(password, sysUser.getPassword())) {
            return AjaxResult.error("用户名或密码错误");
        }
        LoginSysUser loginUser=new LoginSysUser();
        BeanUtils.copyProperties(sysUser, loginUser);
        loginUser.setId(sysUser.getUserId());
        loginUser.setRoles(sysRoleList);
        ajax.put(Constants.TOKEN, sysUserTokenService.createToken(loginUser));
        ajax.put(Constants.UUID, sysUser.getUserId());
        ajax.put(Constants.LOGIN_NAME,sysUser.getNickName());
        ajax.put(Constants.URL_SERVER, configService.selectConfigByKey(InitConstant.PRO_IMG_KEY));
        return ajax;
    }


}
